<template>
  <el-dialog :title="dialogTitle" :visible.sync="visible" width="40%" @close="closeDialog">
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="站点名称" prop="siteName">
        <el-input v-model="formData.siteName"></el-input>
      </el-form-item>
      <el-form-item label="域名" prop="domain">
        <el-input v-model="formData.domain"></el-input>
      </el-form-item>
      <el-form-item label="协议" prop="protocol">
        <el-select v-model="formData.protocol" placeholder="请选择协议">
          <el-option label="HTTP" value="http"></el-option>
          <el-option label="HTTPS" value="https"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="端口" prop="port">
        <el-input-number v-model="formData.port" :min="1" :max="65535"></el-input-number>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择状态">
          <el-option label="启用" value="启用"></el-option>
          <el-option label="禁用" value="禁用"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    dialogTitle: {
      type: String,
      required: true
    },
    formData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      rules: {
        siteName: [
          { required: true, message: '请输入站点名称', trigger: 'blur' }
        ],
        domain: [
          { required: true, message: '请输入域名', trigger: 'blur' }
        ],
        protocol: [
          { required: true, message: '请选择协议', trigger: 'change' }
        ],
        port: [
          { required: true, message: '请输入端口', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    };
  },
  methods: {
    closeDialog() {
      this.$emit('update:visible', false);
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('submit', this.formData);
          this.closeDialog();
        } else {
          console.log('验证失败');
          return false;
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.el-form {
  padding-right: 20px;
}
</style>
